<?php

namespace api\modules\v1\forms\member;

use Yii;
use yii\base\Model;
use common\models\member\Member;

/**
 *
 * Class MemberForm
 * @package api\modules\v1\forms\member
 */
class MemberForm extends Model
{
    /**
     * 用户ID
     *
     * @var
     */
    public $id;

    /**
     * 真实姓名
     *
     * @var
     */
    public $realname;

    /**
     * 手机号
     *
     * @var
     */
    public $mobile;

    /**
     * 性别
     *
     * @var
     */
    public $gender;

    /**
     * 头像
     *
     * @var
     */
    public $head_img;

    /**
     * 密码
     *
     * @var
     */
    public $password;

    /**
     * 新密码
     *
     * @var
     */
    public $new_password;

    /**
     * 确认密码
     *
     * @var
     */
    public $confirm_password;


    /**
     * 用户信息
     *
     * @var
     */
    protected $memberInfo;


    public function rules()
    {
        return [
            // 参数验证
            [['id', 'realname', 'mobile'], 'required', 'message' => '{attribute}不能为空', 'on' => ['edit']],
            [['id', 'password', 'new_password', 'confirm_password'], 'required', 'message' => '{attribute}不能为空', 'on' => ['edit_password']],

            [['id'], 'checkId', 'skipOnEmpty' => false, 'on' => ['edit', 'edit_password']],
            [['realname'], 'checkRealname', 'skipOnEmpty' => false, 'on' => ['edit']],
            [['mobile'], 'checkMobile', 'skipOnEmpty' => false, 'on' => ['edit']],
            [['gender'], 'checkGender', 'skipOnEmpty' => false, 'on' => ['edit']],
            [['head_img'], 'checkHeadImg', 'skipOnEmpty' => false, 'on' => ['edit']],
            [['password'], 'checkPassword', 'skipOnEmpty' => false, 'on' => ['edit_password']],
            [['new_password'], 'checkNewPassword', 'skipOnEmpty' => false, 'on' => ['edit_password']],
            [['confirm_password'], 'checkConfirmPassword', 'skipOnEmpty' => false, 'on' => ['edit_password']],
        ];
    }

    public function scenarios()
    {
        $scenarios = parent::scenarios();

        // 编辑
        $scenarios['edit'] = ['id', 'realname', 'mobile', 'gender', 'head_img'];

        // 修改密码
        $scenarios['edit_password'] = ['id', 'password', 'new_password', 'confirm_password'];

        return $scenarios;
    }

    /**
     * @return array
     */
    public function attributeLabels()
    {
        return [
            'id' => '用户UID',
            'realname' => '真实姓名',
            'mobile' => '手机号',
            'gender' => '性别',
            'head_img' => '头像',
            'password' => '密码',
            'new_password' => '新密码',
            'confirm_password' => '确认密码',
        ];
    }

    /**
     * 检验用户ID
     *
     * @param $attribute
     * @return bool
     */
    public function checkId($attribute)
    {
        if ($this->hasErrors()) {
            return true;
        }

        $member = $this->getMember();
        if (empty($member)) {
            $this->addError($attribute, '参数错误[id_error]');
            return true;
        }
    }

    /**
     * 检验姓名
     *
     * @param $attribute
     * @return bool
     */
    public function checkRealname($attribute)
    {
        if ($this->hasErrors()) {
            return true;
        }

        if (mb_strlen($this->realname) > 40) {
            $this->addError($attribute, '姓名内容过长[1-40字]');
            return true;
        }
    }

    /**
     * 校验手机号
     *
     * @param $attribute
     */
    public function checkMobile($attribute)
    {
        if ($this->hasErrors()) {
            return true;
        }

        if (!preg_match('/^1\d{10}$/', $this->mobile)) {
            $this->addError($attribute, '手机号格式错误[11位数字]');
            return true;
        }

        $member = $this->getMember();
        if ($this->mobile != $member->mobile) {
            $mobile = Yii::$app->services->member->findInfo([
                'mobile' => $this->mobile,
            ]);
            if ($mobile) {
                $this->addError($attribute, '手机号已存在');
                return true;
            }
        }
    }

    /**
     * 校验性别
     *
     * @param $attribute
     */
    public function checkGender($attribute)
    {
        if ($this->hasErrors()) {
            return true;
        }

        if (!in_array($this->gender, [Member::GENDER_UNKNOW, Member::GENDER_MAN, Member::GENDER_WOMAN])) {
            $this->addError($attribute, '参数错误[gender_error]');
            return true;
        }
    }

    /**
     * 校验头像
     *
     * @param $attribute
     */
    public function checkHeadImg($attribute)
    {
        if ($this->hasErrors()) {
            return true;
        }
    }

    /**
     * 校验原密码
     *
     * @param $attribute
     */
    public function checkPassword($attribute)
    {
        if ($this->hasErrors()) {
            return true;
        }

        $member = $this->getMember();
        if (!$member->validatePassword($this->password)) {
            $this->addError($attribute, '原密码错误');
            return true;
        }
    }

    /**
     * 校验新密码
     *
     * @param $attribute
     */
    public function checkNewPassword($attribute)
    {
        if ($this->hasErrors()) {
            return true;
        }

        /*$member = $this->getMember();
        if (!$member->validatePassword($this->password)) {
            $this->addError($attribute, '原密码错误');
            return true;
        }*/
    }

    /**
     * 校验确认密码
     *
     * @param $attribute
     */
    public function checkConfirmPassword($attribute)
    {
        if ($this->hasErrors()) {
            return true;
        }

        if($this->confirm_password != $this->new_password) {
            $this->addError($attribute, '两次密码不一样');
            return true;
        }
    }

    /**
     * 获取用户信息
     *
     * @return Member|null
     */
    public function getMember()
    {
        if (is_null($this->memberInfo)) {
            $this->memberInfo = Yii::$app->services->member->findById($this->id);
        }

        return $this->memberInfo;
    }
}
